package com.lk.satokendemo.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.lk.satokendemo.entity.User;
import com.lk.satokendemo.service.UserService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

@RestController
@RequestMapping("/user/")
public class UserController {

    @Resource
    private UserService userService;

    // 测试登录，浏览器访问： http://localhost:8081/user/doLogin?username=zhang&password=123456
    @PostMapping("doLogin")
    public User doLogin(@RequestBody User user) {
        User u = userService.Login(user);
        Integer id = u.getId();
        StpUtil.login(id);
        return u;
    }

    // 查询登录状态，浏览器访问： http://localhost:8081/user/isLogin
    @RequestMapping("isLogin")
    public String isLogin() {
        return "当前会话是否登录：" + StpUtil.isLogin();
    }

    @GetMapping("logout")
    public String Logout() {
        if (StpUtil.isLogin()) {
            StpUtil.logout();
            return "注销成功";
        }
        return "还未登录";
    }


    @GetMapping("delete")
    public String delete() {
        if (!StpUtil.hasRole("1")) {
            return "权限不足";
        }
        return "删除成功";
    }

}
